package com.scap.authen.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.scap.authen.entity.RoleApp;

@Repository("voRoleAppDao")
public class RoleAppDaoImpl extends AbstractDao implements RoleAppDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getAllRoleApp() {			
			Criteria criteria = getSession().createCriteria(RoleApp.class);
		return (List<Object>)criteria.list();
	}

	@Override
	public boolean insertRoleApp(RoleApp poRoleApp) {
		boolean result = false;		
		try {
			persist(poRoleApp);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}		
		return result;
	}

	@Override
	public void deleteRoleAppByRoleIdSiteId(String vaRoleId, String vaSiteId) {
		Query query = getSession().createQuery("Delete from RoleApp Where vaRoleId = :vaRoleId and vaSiteId = :vaSiteId");
		query.setString("vaRoleId", vaRoleId);
		query.setString("vaSiteId", vaSiteId);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoleApp> findApplicationChecked(String roleId, String siteId) {
		Criteria criteria = getSession().createCriteria(RoleApp.class);
		criteria.add(Restrictions.eq("vaSiteId", siteId));
		criteria.add(Restrictions.eq("vaRoleId", roleId));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> findApplicationByRoleIdSiteId(String roleId, String siteId) {
			
			Query query = getSession().createQuery("Select rapp.vaApplicationId,app.vaApplicationName "
													+ " from RoleApp rapp, Application app "
													+ " where  rapp.vaRoleId = :vaRoleId AND rapp.vaSiteId =:vaSiteId AND rapp.vaApplicationId = app.vaApplicationId ");
			
			query.setString("vaRoleId", roleId);
			query.setString("vaSiteId", siteId);
			List<Object[]>  result = query.list();
			return result;
	}
}
